package org.i_plus.model.dao;

import java.beans.Beans;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import org.i_plus.model.BlogBean;
import org.i_plus.model.BlogDAO;
import org.i_plus.model.MemberBean;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class BlogDAOHibernate implements BlogDAO {
	private SessionFactory sessionFactory;
	
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Override
	public BlogBean selectByBbum(int b_num) {
		Session session = sessionFactory.getCurrentSession();
		BlogBean bean = (BlogBean)session.get(BlogBean.class,b_num);
//		System.out.println(bean.getMemberBean().getAccount());
//		System.out.println(bean.getB_num());
//		System.out.println(bean.getB_time());
//		System.out.println(bean.getContent());
		return bean;
		
	}
	@Override
	public List<BlogBean> selectbyAcc(String account) {
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from BlogBean where MemberBean.Account=:account");
		query.setString("account", account);
		return  (List<BlogBean>)query.list();
	}
	@Override
	public List<BlogBean> selectB() {
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from BlogBean");
		return (List<BlogBean>)query.list();
	}

	@Override
	public void updateB(String account, int b_num, String content,
			Timestamp b_time) {
		Session session = sessionFactory.getCurrentSession();
		BlogBean bean =(BlogBean) session.get(BlogBean.class, b_num);
		MemberBean member= new MemberBean();
		member.setAccount(account);
		bean.setMemberBean(member);
		System.out.println("member"+member);
		bean.setB_num(b_num);
		Date date = new Date();
		Timestamp timestamp = new Timestamp(date.getTime());
		bean.setB_time(timestamp);
//		bean.setB_time(b_time);
		bean.setContent(content);
		session.update(bean);
		
	}

	@Override
	public void insertB(String account, String content,
			Timestamp b_time) {
		Session session = sessionFactory.getCurrentSession();
		BlogBean bean = new BlogBean();
		MemberBean member= new MemberBean();
		member.setAccount(account);
		bean.setMemberBean(member);
		bean.setB_time(b_time);
		bean.setContent(content);
		session.save(bean);
		
		
	}

	@Override
	public void deleteB(int b_num) {
		Session session = sessionFactory.getCurrentSession();
		BlogBean bean=(BlogBean) session.get(BlogBean.class, b_num);
		session.delete(bean);
		
	}
    public static void main(String[] args){
    	ApplicationContext context = new ClassPathXmlApplicationContext("beans.config.xml");
    	SessionFactory session=(SessionFactory) context.getBean("sessionFactory");
		
		session.getCurrentSession().beginTransaction();
		BlogDAOHibernate bdao = (BlogDAOHibernate) context.getBean("blogDAO");
		
//		bdao.insertB("123", "abcd", new java.sql.Timestamp(2, 2, 0, 0, 0, 0, 0));
	//	BlogBean bean = bdao.selectB(6);
//		List<BlogBean> list=bdao.selectbyAcc("123");
//		for(BlogBean blogs:list)
//		{
//			System.out.println(blogs.getContent());
//		}
//		bdao.updateB("456",19, "aaaa", new java.sql.Timestamp(2, 2, 2, 2, 2, 0, 0));
//		bdao.deleteB(5);
		session.getCurrentSession().getTransaction().commit();
    }

	

	
	
}
